<template>
	<view class='warp' style="background-color: #FFFFFF;">
		<view class="form box-s1">


			<view><text class="red">*</text>门店名称：<input type="text" placeholder="请输入门店名称" v-model="nickname"
					:disabled="disabled"></view>


			<view><text class="red">*</text>门店手机号：<input type="number" maxlength="11" placeholder="请输入门店手机号"
					v-model="phone" :disabled="disabled"></view>

			<view><text class="red">*</text>门店详细位置： <text class='iconfont' style="color: #1296db;font-size:36rpx;"
					@click="addrClick">
					&#xe612; <text style="font-size: 20rpx;">(点击选择门店位置)</text></text><textarea type="number"
					placeholder="请输入门店详细位置" v-model="address" :disabled="disabled"></textarea>
			</view>

			<view>
				<view class="userId">
					<text class="red">*</text>门店照片： <text class="del red" v-if="url">*长按图片进行删除</text>
					<view>
						<view v-if="!url">
							<view class="iconfont img-sfz" @click="choose">&#xe61f;</view>

						</view>
						<image v-else :src="url" mode="" @longtap="del()"></image>
					</view>
					<view>
					</view>
				</view>
			</view>
		</view>

		<view class="btn" style="margin-bottom: 100rpx;">
			<button @click="confirm">提交录入</button>
			<!-- <button @click="back">取消添加</button> -->
		</view>
	</view>
</template>

<script>
	import {
		inputUser,
	} from '../../../api/api.js'
	import combox from "../../../components/uni-ui/lib/uni-combox/uni-combox.vue"

	export default {
		components: {
			combox
		},
		data() {
			return {
				list: [],
				keyword1: "",
				index1: 0,
				url: "",
				nickname: "",
				phone: "",
				longitude: "",
				latitude: "",
				address: "",

				uploadUrl: ""
			}

		},
		created() {
			this.uploadUrl = uni.getStorageSync('uploadUrl')

		},
		methods: {
			addrClick() { // 地址选择事件
				const _this = this;
				uni.chooseLocation({
					success: (res) => {
						console.log(res, 1)
						let {
							address,
							name,
							latitude,
							longitude
						} = res;
						_this.longitude = longitude
						_this.latitude = latitude
						_this.address = address + name;
					},
					fail: (res) => {
						console.log(res, 2)
					}
				})
			},
			// 返回
			back() {
				uni.navigateTo({
					url: "/pages/index/index"
				})
			},
			// 上传图片
			choose() {
				// 选择图片文件
				console.log('点击', this.disabled)
				if (this.disabled) return
				let that = this
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						that.url = res.tempFilePaths[0]
						uni.uploadFile({
							url: that.uploadUrl, //仅为示例，非真实的接口地址
							filePath: that.url,
							name: 'file',
							header: {
								"Authori-zation": 'Bearer ' + that.token
							},
							methods: 'post',
							success: (res) => {
								let data = JSON.parse(res.data)
								console.log('上传地址', data)
								if (data.status == 200) {
									that.url = data.data.url
									uni.showToast({
										title: data.msg,
										icon: "none"
									})
								}
							}
						});
					}
				});
			},
			// 删除
			del() {
				if (this.disabled) return
				uni.showModal({
					title: "提示",
					content: "确定删除该图片吗?",
					showCancel: true,
					success: (res) => {
						if (res.confirm) {
							this.url = ''
							return
						}
						uni.showToast({
							title: "操作取消!",
							icon: "none"
						})
					}

				})
			},
			// 确认添加
			confirm() {

				if (!this.url) return uni.showToast({
					title: "请上传图片!",
					icon: "none"
				})



				if (!this.nickname || !this.phone || !this.longitude || !this.url || !this.address) return uni.showToast({
					title: "请完善信息!",
					icon: "none"
				});
				if (!(/^1(3|4|5|7|8|9|6)\d{9}$/i.test(this.phone))) uni.showToast({
					title: "请输入正确的手机号码!",
					icon: "none"
				});

				let par = {
					nickname: this.nickname,
					phone: this.phone,
					longitude: this.longitude,
					latitude: this.latitude,
					avatar: this.url,
					address: this.address,
				}
				inputUser(par).then(res => {
					if (res.status == 200) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/index/index'
							})
						}, 300)
						return;
					}
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})

				})


			},



		}

	}
</script>

<style scoped lang='less'>
	.form {
		width: 80%;
		padding: 30rpx;
		padding-top: 0;
		border-radius: 20rpx;
		margin-top: 30rpx;
		margin-left: 50%;
		transform: translateX(-50%);

		.addrIcon {
			position: absolute;
			right: 36upx;
			top: 200rpx;

		}

		input {
			display: inline-block;
			width: 65%;
			vertical-align: middle;
		}

		view {
			line-height: 80rpx;
			margin-top: 30rpx;
			/* border-bottom: 2rpx solid #f0efef; */
			color: #333;
		}

		>view:last-of-type {
			border: none;
		}
	}

	.btn-group {
		width: 100%;
		display: flex;
		height: 60rpx;
		justify-content: center;
		align-items: center;
		line-height: 60rpx;
		position: absolute;
		bottom: 0rpx;


	}

	.img-sfz {
		margin-top: 50rpx;
		text-align: center;
		color: #409EFF !important;
		font-size: 200rpx;
	}

	.del {
		color: #FD3833;
		font-size: 20rpx;
	}
</style>
